# include "pool.h"

pthread_t getid(){
    return pthread_self();
}
int thread_num = 0;
void * task(void * arg){
    int id = *(int*)arg;
    printf("thread %d running.\tthread_id %u\n", id, pthread_self());
    int count = 0;
    while(count++ < 2){
        sleep(rand()%2);
        lamport_lock(id);
        if(thread_num)
            printf("last thread is %d.\n", thread_num);
        printf("least thread is %u.\n", id);
        thread_num = id;
        lamport_unlock(id);
    }
    return NULL;
}

// void * task(void * arg){
//     int id = *(int*)arg;
//     printf("thread %d running.\tthread_id %u\n", id, pthread_self());
//     int count = 0;
//     sleep(rand()%2);
//     return NULL;
// }

int main(){
    tPool * pool;
    if(create_pool(&pool, 10)){
        printf("create_pool failed!\n");
        exit(1);
    }

    int test[30];
    for(int i = 0; i < 30; i++){
        // printf("add task %d\n", i+1);
        test[i] = i+1;
        add_task(pool, task, (void*)&test[i]);
    }

    sleep(3);

    destroy_pool(pool);

    return 0;
}